﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OAclient.Models;
using ServiceApplication.Service.BBS;
using CommonLibrary.DataModel.BBS;
using OAclient.Common;
using OAclient.Filters;

namespace OAclient.Controllers
{
    [VaildateLogin]
    [ExceptionFilter]
    [LoggerFilter]
    public class BBSController : Controller
    {
        //
        // GET: /BBS/

        BBSService bbsService = new BBSService();
        public const Int32 PageSize = 15;

        public ActionResult BBSMain()
        {
            return View();
        }

        public ActionResult QueryAllHotCard()
        {
            ViewData["dt_cards"] = bbsService.QueryAllHotCard();
            return PartialView("CardList");
        }

        public ActionResult QueryAllInitCard(UserModel userModel, Int32 page)
        {
            if (userModel.user.auth_id == 1)
                ViewData["mng"] = "true";
            var card = bbsService.QueryAllInitCard();
            List<InitCardEntity> cards = new List<InitCardEntity>();
            if (card != null)
                cards = bbsService.QueryAllInitCard().Skip((page - 1) * PageSize).Take(PageSize).ToList();

            ViewData["dt_cards"] = cards;
            ViewData["pageInfo"] = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = cards.Count() == 0 ? 1 : cards.Count
            };
            return PartialView("CardList");
        }

        [HttpPost]
        public ActionResult IniteCard(UserModel userModel, String text, String title)
        {
            InitCardEntity initCardEnty = new InitCardEntity
            {
                CardText = text,
                CardTitle = title,
                InitTime = DateTime.Now.ToString("yyyy-MM-dd"),
                CardState = "Normal",
                ReplyCount = "0",
                User = new CommonLibrary.DataModel.Basic.UserEntity
                {
                    user_id = userModel.user.user_id
                }
            };
            bbsService.AddInitCard(initCardEnty);

            return JavaScript("document.location='/BBS/BBSMain'");
        }

        [HttpGet]
        public ActionResult CheckCard(UserModel userModel, String inCardId, Int32 page)
        {
            if (userModel.user.auth_id == 1)
                ViewData["mng"] = "true";
            var cardDetail = bbsService.QueryInitCard(inCardId);
            Dictionary<String, Object> dicCard = new Dictionary<string, Object>();
            dicCard["card_id"] = cardDetail.CardId;
            dicCard["card_title"] = cardDetail.CardTitle;
            dicCard["card_text"] = Server.HtmlDecode(cardDetail.CardText);
            dicCard["user_name"] = cardDetail.User.user_id.ToString();
            dicCard["init_time"] = cardDetail.InitTime;
            dicCard["reply_card"] = String.Empty;

            var replyCards = cardDetail.ReplyCards;
            List<ReplyCardEntity> cards = new List<ReplyCardEntity>();
            if (replyCards != null)
            {
                cards = replyCards.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                dicCard["reply_card"] = cardDetail.ReplyCards;
            }

            ViewData["card_info"] = dicCard;
            ViewData["pageInfo"] = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = cards.Count == 0 ? 1 : cards.Count
            };
            return View("CardDetail");
        }

        [ValidateInput(false)]
        public ActionResult ReplyCard(UserModel userModel, String inCardId, String text)
        {
            ReplyCardEntity replyCardEnty = new ReplyCardEntity
            {
                CardText = text,
                InitCardId = inCardId,
                ReplyTime = DateTime.Now.ToString("yyyy-MM-dd"),
                User = new CommonLibrary.DataModel.Basic.UserEntity
                {
                    user_id = userModel.user.user_id
                }
            };
            bbsService.AddReplyCard(replyCardEnty);
            return JavaScript("document.location='/BBS/CheckCard?inCardId=" + inCardId + "&page=1'");
        }

        public ActionResult MarkHot(String inCardId)
        {
            var result = bbsService.MarkHotCard(inCardId, DateTime.Now.ToString("yyyy-MM-dd"));
            return RedirectToAction("BBSMain");
        }

        public ActionResult DelInitCard(String inCardId)
        {
            bbsService.DelInitCard(inCardId);
            return View("BBSMain");
        }

        public ActionResult DelReplyCard(String inCardId, String reCardId)
        {
            bbsService.DelReplayCard(reCardId);
            return RedirectToAction("CheckCard", new { inCardId = inCardId, page = 1 });
        }

        public ActionResult FckSurface()
        {
            return PartialView("Fckeditor");
        }

        public ActionResult GetMyCards(UserModel userModel, Int32 page)
        {
            var cards = bbsService.QueryMyCards(userModel.user.user_id.ToString());

            List<InitCardEntity> cardList = new List<InitCardEntity>();
            if (null != cards && cards.Count > 0)
                cardList = cards.ToList();
            ViewData["myCards"] = cardList;
            ViewData["pageInfo"] = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = cards.Count == 0 ? 1 : cards.Count
            };
            return View();
        }
    }
}
